package com.chrisgammage.gtwit.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Created by IntelliJ IDEA.
 * User: gammagec
 * Date: 5/30/12
 * Time: 10:14 PM
 */
public class GTwitTestView extends ResizeComposite {

    interface GTwitTestViewUiBinder extends UiBinder<LayoutPanel, GTwitTestView> {
    }

    private static GTwitTestViewUiBinder ourUiBinder = GWT.create(GTwitTestViewUiBinder.class);

    @UiField
    GTwitWidget gTwitWidget;
    @UiField
    Button goButton;
    @UiField
    TextBox countField;
    @UiField
    TextBox userName;

    public GTwitTestView() {
        initWidget(ourUiBinder.createAndBindUi(this));
        gTwitWidget.fetchTweets();
    }

    @UiHandler("goButton")
    public void onGoButtonClicked(MouseUpEvent event) {
        int count = 0;
        try {
            count = Integer.valueOf(countField.getText());
        } catch(NumberFormatException e) {
            Window.alert("Couldn't convert count to an Integer");
            return;
        }
        gTwitWidget.setCount(count);
        gTwitWidget.setUserId(userName.getText());
        gTwitWidget.fetchTweets();
    }
}